﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medical.Business;
using System.Data;
using Medical.Entity;

/// <summary>
/// 文章列表页面
/// </summary>
public partial class ArticleList : WebPage
{
    /// <summary>
    /// 文章类型
    /// </summary>
    protected ArticelTypeEnum eType;

    protected int nSortID = 0;

    protected int nPSortID=0;

    protected int nPageIndex = 1;

    protected bool bImg = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["page"] != null)
            {
                nPageIndex = Convert.ToInt32(Request.QueryString["page"]);
            }
            InitConfig();
            InitList();
        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        if (eType == ArticelTypeEnum.Disease)
        {
            if (nPSortID > 0)
            {
                DataTable dtAd = Until.GetWebBanner();
                if (dtAd != null && dtAd.Rows.Count > 0)
                {
                    DataRow[] temdrList = dtAd.Select("OrderNum=" + nPSortID.ToString());
                    if (temdrList.Length > 0)
                    {
                        Image imgHead = Master.FindControl("imgHead") as Image;
                        imgHead.ImageUrl = "/" + WebConfigInfo.FilePlace + "d/" + temdrList[0]["ImgPlace"].ToString();
                    }
                }
            }
        }
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitConfig()
    {
        if (RouteData.Values["urlname"] != null)
        {
            string strUrlName = RouteData.Values["urlname"].ToString();
            DataTable dtSort = new BisSortInfo().GetSortInfo(strUrlName);
            if (dtSort != null && dtSort.Rows.Count > 0)
            {
                string strFurlName = string.Empty;
                if (dtSort.Rows[0]["PID"].ToString() != string.Empty)
                {
                    nPSortID = Convert.ToInt32(dtSort.Rows[0]["PID"].ToString());
                    strFurlName = dtSort.Rows[0]["PPageUrlName"].ToString();
                    ltlPageUrl.Text = "<a href=\"/" + dtSort.Rows[0]["PPageUrlName"].ToString() + "/\" title=\"" + dtSort.Rows[0]["PName"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(dtSort.Rows[0]["PName"].ToString()) + "</a> &gt; ";
                }
                eType = (ArticelTypeEnum)Convert.ToInt32(dtSort.Rows[0]["Type"]);
                nSortID = Convert.ToInt32(dtSort.Rows[0]["ID"]);
                ltlPageUrl.Text += Server.HtmlEncode(dtSort.Rows[0]["Name"].ToString());
                DataRow[] drList;
                DataTable dtSeoInfo = Until.GetSEO();
                switch (eType)
                {
                    case ArticelTypeEnum.Technology:
                        drList = dtSeoInfo.Select("Type=4");
                        if (drList.Length > 0)
                            SetPageInfo(drList[0]["PageTitle"].ToString() == string.Empty ? ("前沿技术_" + strWebTitle) : drList[0]["PageTitle"].ToString(), drList[0]["PageKeywords"].ToString(), drList[0]["PageDescription"].ToString());
                        break;
                    case ArticelTypeEnum.Guide:
                        drList = dtSeoInfo.Select("Type=8");
                        if (drList.Length > 0)
                            SetPageInfo(drList[0]["PageTitle"].ToString() == string.Empty ? ("成功案例_" + strWebTitle) : drList[0]["PageTitle"].ToString(), drList[0]["PageKeywords"].ToString(), drList[0]["PageDescription"].ToString());
                        break;
                    case ArticelTypeEnum.Health:
                        drList = dtSeoInfo.Select("Type=9");
                        if (drList.Length > 0)
                            SetPageInfo(drList[0]["PageTitle"].ToString() == string.Empty ? ("症状列表_" + strWebTitle) : drList[0]["PageTitle"].ToString(), drList[0]["PageKeywords"].ToString(), drList[0]["PageDescription"].ToString());
                        break;
                    case ArticelTypeEnum.Case:
                        drList = dtSeoInfo.Select("Type=10");
                        if (drList.Length > 0)
                            SetPageInfo(drList[0]["PageTitle"].ToString() == string.Empty ? ("成功案例_" + strWebTitle) : drList[0]["PageTitle"].ToString(), drList[0]["PageKeywords"].ToString(), drList[0]["PageDescription"].ToString());
                        break;
                    default:
                        string strPageTitle = string.IsNullOrEmpty(dtSort.Rows[0]["PageTitle"].ToString()) ? (dtSort.Rows[0]["Name"].ToString() + "_" + strWebTitle) : dtSort.Rows[0]["PageTitle"].ToString();
                        SetPageInfo(strPageTitle, dtSort.Rows[0]["PageKeywords"].ToString(), dtSort.Rows[0]["PageDescription"].ToString());
                        break;

                }

                anpPage.EnableUrlRewriting = true;
                anpPage.UrlRewritePattern = @"~" + (strFurlName != string.Empty ? "/" + strFurlName : "") + "/" + strUrlName + "/?page={0}";
            }
            else
            {
                Response.Redirect("/404.html");
            }
        }
        else
        {
            Response.Redirect("/404.html");
        }
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    protected void InitList()
    {
        int nRecordCount = 0;
        int nPageCount = 0;
        //文章信息列表绑定
        int nOrderType = 0;
        DataTable dt = new BisArticle().GetArticleList(nSortID, 0, eType, string.Empty, string.Empty, string.Empty, nOrderType, nPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        anpPage.RecordCount = nRecordCount;
        //if (eType == ArticelTypeEnum.Technology)
        //{
        //    bImg = true;
        //    rptArticle1.DataSource = dt;
        //    rptArticle1.DataBind();
        //}
        //else
        //{
            rptArticle.DataSource = dt;
            rptArticle.DataBind();
       // }
        dt.Dispose();
    }
    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptArticle_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        Literal ltlInfo = e.Item.FindControl("ltlInfo") as Literal;
        string strInfo = strInfo = Common.NoHTML(dtSort.Rows[e.Item.ItemIndex]["Info"].ToString());
        if (eType == ArticelTypeEnum.Technology)
        {
            ltlInfo.Text = (strInfo.Length > 160 ? strInfo.Substring(0, 160) : strInfo) + "…";
        }
        else
        {
            ltlInfo.Text = (strInfo.Length > 80 ? strInfo.Substring(0, 80) : strInfo) + "…";
        }
    }
}